package com.alogic.xscript.plugins;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.*;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

/**
 * 对指定的变量值进行环境变量填充
 *
 * @since 1.6.14.13 [2021708 duanyy] <br>
 */
public class EnvSubstr extends AbstractLogiclet {
    protected String $id;
    protected String $key;

    protected List<Pair<String,String>> envs = new ArrayList<Pair<String,String>>();

    public EnvSubstr(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p) {
        super.configure(p);
        $id = PropertiesConstants.getString(p,"id",$id);
        $key = PropertiesConstants.getString(p,"key",$key);
    }

    @Override
    public void configure(Element e, Properties p) {
        Properties props = new XmlElementProperties(e,p);
        configure(props);

        NodeList nodeList = XmlTools.getNodeListByPath(e, "env");
        for (int i = 0 ;i < nodeList.getLength() ; i ++){
            Node n = nodeList.item(i);
            if (n.getNodeType() != Node.ELEMENT_NODE){
                continue;
            }

            Element elem = (Element)n;
            String id = elem.getAttribute("id");
            String value = elem.getAttribute("value");
            if (StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(value)){
                envs.add(new Pair.Default<String,String>(id,value));
            }
        }
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String id = PropertiesConstants.transform(ctx,$id,null);
        if (StringUtils.isNotEmpty(id)){
            String key = PropertiesConstants.transform(ctx,$key,id);
            if (envs.isEmpty()) {
                String content = PropertiesConstants.getString(ctx, key, "");
                if (StringUtils.isNotEmpty(content)) {
                    ctx.SetValue(id, content.replaceAll("\\$dollar", Matcher.quoteReplacement("$")));
                }
            }else{
                DefaultProperties envHolder = new DefaultProperties("default", ctx);
                for (Pair<String,String> pair:envs){
                    String envId = pair.key();
                    String envValue = PropertiesConstants.transform(ctx,pair.value(),null);
                    if (StringUtils.isNotEmpty(envId) && StringUtils.isNotEmpty(envValue)){
                        envHolder.SetValue(envId,envValue);
                    }
                }
                String content = PropertiesConstants.getString(envHolder, key, "");
                if (StringUtils.isNotEmpty(content)) {
                    content = content.replaceAll("\\$dollar", Matcher.quoteReplacement("$"));
                    ctx.SetValue(id, content);
                }
            }
        }
    }
}
